package com.hanhai.material.common.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
/**
 * Project：health
 * Create by CC
 * Description:封装返回的JSON数据
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {

    private Boolean success;

    private Integer code;

    private String message;

    private T data;

    public Result(boolean success) {
        this.success = success;
        this.code = success ? ResultCode.SUCCESS.getCode() : ResultCode.FAIL.getCode();
        this.message = success ? ResultCode.SUCCESS.getMessage() : ResultCode.FAIL.getMessage();
    }

    public Result(boolean success, ResultCode resultCode) {
        this.success = success;
        this.code = success ? ResultCode.SUCCESS.getCode() : (resultCode == null ? ResultCode.FAIL.getCode() : resultCode.getCode());
        this.message = success ? ResultCode.SUCCESS.getMessage() : (resultCode == null ? ResultCode.FAIL.getMessage() : resultCode.getMessage());
    }

    public Result(boolean success, T data) {
        this.success = success;
        this.code = success ? ResultCode.SUCCESS.getCode() : ResultCode.FAIL.getCode();
        this.message = success ? ResultCode.SUCCESS.getMessage() : ResultCode.FAIL.getMessage();
        this.data = data;
    }

    public Result(boolean success, ResultCode resultCode, T data) {
        this.success = success;
        this.code = success ? ResultCode.SUCCESS.getCode() : (resultCode == null ? ResultCode.FAIL.getCode() : resultCode.getCode());
        this.message = success ? ResultCode.FAIL.getMessage() : (resultCode == null ? ResultCode.FAIL.getMessage() : resultCode.getMessage());
        this.data = data;
    }

    public Result(boolean success, String message) {
        if (success) {
            this.code = 200;
        } else {
            this.code = 400;
        }
        this.success = success;
        this.message = message;
    }

}